﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mystery.SCNR;

namespace Mystery
{
    public partial class PlacementSelector : Form
    {
        public AIZone[] AIZones
        { get; private set; }

        public AISquad[] AISquads
        { get; private set; }

        public void SetInfo(AISquad[] aiSquads, AIZone[] aiZones)
        {
            AIZones = aiZones;
            AISquads = aiSquads;

            lstPlacements.Items.Clear();
            lstPlacements.Items.AddRange(AISquads);
        }

        public PlacementSelector()
        {
            InitializeComponent();
        }

        private void cblstPlacements_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            AISquads[e.Index].Ignore = (e.NewValue != CheckState.Checked);
        }

        private void PlacementSelector_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Visible = false;
            }
        }

        private void PlacementSelector_KeyDown(object sender, KeyEventArgs e)
        {
            short newIndex = -1;
            switch ((Keys)e.KeyValue)
            {
                case Keys.N:
                case Keys.Delete:
                    foreach (AISquad ais in lstPlacements.SelectedItems)
                        ais.Remove = !ais.Remove;
                    break;
                case Keys.C:
                case Keys.Space:
                    foreach (AISquad ais in lstPlacements.SelectedItems)
                        ais.Ignore = !ais.Ignore;
                    break;
                case Keys.NumPad0:
                case Keys.D0: newIndex = 0; break;
                case Keys.NumPad1:
                case Keys.D1: newIndex = 1; break;
                case Keys.NumPad2:
                case Keys.D2: newIndex = 2; break;
                case Keys.NumPad3:
                case Keys.D3: newIndex = 3; break;
                case Keys.NumPad4:
                case Keys.D4: newIndex = 4; break;
                case Keys.NumPad5:
                case Keys.D5: newIndex = 5; break;
                case Keys.NumPad6:
                case Keys.D6: newIndex = 6; break;
                case Keys.NumPad7:
                case Keys.D7: newIndex = 7; break;
                case Keys.NumPad8:
                case Keys.D8: newIndex = 8; break;
                case Keys.NumPad9:
                case Keys.D9:
                    foreach (AISquad ais in lstPlacements.SelectedItems)
                        ais.NetgameHill = !ais.NetgameHill;
                    break;
            }

            if (newIndex >= 0)
            {
                foreach (AISquad ais in lstPlacements.SelectedItems)
                    ais.NetgameBaseTeam = newIndex;
            }
            UpdateToString(lstPlacements);
        }

        private void UpdateToString(ListBox listBox)
        {
            int count = listBox.Items.Count;
            listBox.SuspendLayout();
            for (int i = 0; i < count; i++)
            {
                listBox.Items[i] = listBox.Items[i];
            }
            listBox.ResumeLayout();
        }
    }
}
